import Mock from "mockjs";

interface user {
  userPhoneNumber: string;
  identifyCode: string;
}

interface ResponseMessage {
  status: number;
  msg: string;
}

const userList:user[] = [
  {
    userPhoneNumber: "22121212",
    identifyCode: "1212",
  },
  {
    userPhoneNumber: "12122212",
    identifyCode: "2212",
  }
];

Mock.mock({
  userList,
})

Mock.mock("/api/user/login", "post", (options: { body: string; }) => {
  const {userPhoneNumber, identifyCode} = JSON.parse(options.body);

  const result: user|undefined = userList.find((element) => {
    return element.userPhoneNumber == userPhoneNumber
  })

  if (!result) {
    return <ResponseMessage>{
      status: -1,
      msg: "用户不存在",
    }
  } else {

    if (result.identifyCode != identifyCode) {
      return <ResponseMessage>{
        status: -2,
        msg: "用户密码错误",
      }
    } else {
      return <ResponseMessage>{
        status: 0,
        msg: "登录成功"
      }
    }
  }
})

export default Mock;
export type { ResponseMessage}